package com.tarena.lbs.business.dao.repository;

import com.tarena.lbs.business.pojo.po.StoreSearchPO;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;

public interface StoreESRepository extends ElasticsearchRepository<StoreSearchPO, Integer> {
    
    /**
     * 根据地理位置查询范围内的店铺
     * @param lat 用户纬度
     * @param lon 用户经度
     * @param distance 距离（单位：km）
     * @param pageable 分页信息
     * @return 店铺列表
     */
    @Query("{\n" +
           "  \"bool\": {\n" +
           "    \"filter\": {\n" +
           "      \"geo_distance\": {\n" +
           "        \"distance\": \"?2 km\",\n" +
           "        \"location\": {\n" +
           "          \"lat\": ?0,\n" +
           "          \"lon\": ?1\n" +
           "        }\n" +
           "      }\n" +
           "    }\n" +
           "  }\n" +
           "}")
    Page<StoreSearchPO> findStoresWithinDistance(Double lat, Double lon, Double distance, Pageable pageable);
    
    /**
     * 根据地理位置查询范围内的店铺（无分页）
     */
    @Query("{\n" +
           "  \"bool\": {\n" +
           "    \"filter\": {\n" +
           "      \"geo_distance\": {\n" +
           "        \"distance\": \"?2 km\",\n" +
           "        \"location\": {\n" +
           "          \"lat\": ?0,\n" +
           "          \"lon\": ?1\n" +
           "        }\n" +
           "      }\n" +
           "    }\n" +
           "  }\n" +
           "}")
    List<StoreSearchPO> findStoresWithinDistance(Double lat, Double lon, Double distance);
}